package com.nut.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nut.course.api.req.CourseCheckReq;
import com.nut.course.api.req.CourseNum;
import com.nut.course.api.req.CourseOrderReq;
import com.nut.course.common.Holder;
import com.nut.course.common.Result;
import com.nut.course.entity.Course;
import com.nut.course.entity.CourseOrder;
import com.nut.course.entity.CourseRecord;
import com.nut.course.entity.PartnerCourse;
import com.nut.course.enums.CourseStatusEnum;
import com.nut.course.interceptor.UserJwt;
import com.nut.course.mapper.BizCourseMapper;
import com.nut.course.mapper.BizCourseOrderMapper;
import com.nut.course.mapper.BizCourseRecordMapper;
import com.nut.course.mapper.BizPartnerCourseMapper;
import com.nut.course.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private BizCourseMapper bizCourseMapper;

    @Autowired
    private BizCourseRecordMapper bizCourseRecordMapper;

    @Autowired
    private BizPartnerCourseMapper bizPartnerCourseMapper;

    @Autowired
    private BizCourseOrderMapper bizCourseOrderMapper;

    @Override
    public Result<IPage<Course>> list(String userId, String depart, String status, String name, Integer pageNo, Integer pageSize) {

        Page<Course> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNo);

        String[] statusIds = null;
        if (StringUtils.hasLength(status)) {
            statusIds = status.split(",");
        }

        String[] departIds = null;
        if (StringUtils.hasLength(depart)) {
            departIds = depart.split(",");
        }

        List<Course> list = bizCourseMapper.selectListPage(page, userId, departIds, statusIds, name);
        page.setRecords(list);
        return Result.ok(page);
    }

    @Override
    public Result<Course> getById(Integer id) {
        Course course = bizCourseMapper.selectById(id);
        return Result.ok(course);
    }

    @Override
    public Result add(Course course) {
        UserJwt userJwt = Holder.USER_JWT_THREAD_LOCAL.get();
        course.setUserId(userJwt.getUserId());
        bizCourseMapper.insert(course);
        return Result.ok();
    }

    @Override
    public Result updateById(Course course) {

        if (course.getStatus() == CourseStatusEnum.CHECK_WAIT_1.getValue()) {
            course.setSubmitTime(new Date());
            course.setCheckTime1(null);
            course.setCheckTime2(null);
        }

        bizCourseMapper.updateById(course);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        bizCourseMapper.deleteBatchIds(ids);
        return Result.ok();
    }

    @Override
    public Result check(CourseCheckReq courseCheckReq) {

        Integer courseId = courseCheckReq.getCourseId();
        Course course = bizCourseMapper.selectById(courseId);
        if (course == null) {
            return Result.fail("未找到课程");
        }

        Integer action = courseCheckReq.getAction();
        String reason = courseCheckReq.getReason();

        CourseStatusEnum newStatus = null;
        String level = null;
        CourseStatusEnum statusEnum = CourseStatusEnum.valueOf(course.getStatus());

        //1级待审核
        if (statusEnum == CourseStatusEnum.CHECK_WAIT_1) {
            if (action == 2) {
                newStatus = CourseStatusEnum.CHECK_WAIT_2;
            } else if (action == 3) {
                newStatus = CourseStatusEnum.CHECK_FAIL_1;
            }
            level = "一审:";
        }
        //2级待审核
        else if (statusEnum == CourseStatusEnum.CHECK_WAIT_2) {
            if (action == 2) {
                newStatus = CourseStatusEnum.WAIT_PUBLISH;
            } else if (action == 3) {
                newStatus = CourseStatusEnum.CHECK_FAIL_2;
            }
            level = "二审:";
        }

        if (newStatus == null) {
            return Result.fail("状态不对");
        }

        Course update = new Course();
        update.setId(courseId);
        update.setStatus(newStatus.getValue());
        if (statusEnum == CourseStatusEnum.CHECK_WAIT_1) {
            update.setCheckTime1(new Date());
            update.setCheckTime2(null);
        } else {
            update.setCheckTime2(new Date());
        }
        bizCourseMapper.updateById(update);

        UserJwt userJwt = Holder.USER_JWT_THREAD_LOCAL.get();
        CourseRecord record = new CourseRecord();
        record.setCourseId(courseId);
        record.setUserId(userJwt.getUserId());
        record.setUserName(userJwt.getUsername());
        record.setAction(action);
        record.setRemark(level + reason);
        bizCourseRecordMapper.insert(record);

        return Result.ok();
    }

    @Override
    public Result<List<CourseRecord>> records(Integer id) {
        List<CourseRecord> list = bizCourseRecordMapper.selectList(
                new LambdaQueryWrapper<CourseRecord>().eq(CourseRecord::getCourseId, id).orderByDesc(CourseRecord::getCreateTime));
        return Result.ok(list);
    }

    public static void main(String[] args) {

        String dd = "1,2,3";

        String[] split = dd.split(",");
        System.out.println(split);

        dd = "1";

        String[] split2 = dd.split(",");
        System.out.println(split2);

    }

}
